package com.achao.service.impl;

import com.achao.dao.AdminUserDao;
import com.achao.dao.RoleDao;
import com.achao.service.AdminUserService;
import com.achao.utils.MD5Utils;
import com.achao.utils.MybatisUtils;
import com.achao.utils.ParamUtils;
import com.achao.utils.StringUtils;
import com.achao.view.AdminLoginView;
import com.achao.view.UserManageView;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;

/**
 * @Author: achao
 * @DateTime: 2022/7/24 20:25
 * @Description: TODO
 */
public class AdminUserServiceImpl implements AdminUserService {

    @Override
    public void login() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminUserDao adminUserDao = sqlSession.getMapper(AdminUserDao.class);
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);

        AdminLoginView instance = AdminLoginView.getInstance();
        String userName = instance.getUserNameText().getText();
        String userPwd = instance.getUserPwdText().getText();
        if (StringUtils.isNotEmpty(userName) && StringUtils.isNotEmpty(userPwd)) {
            int id = adminUserDao.getIdByAdminName(userName);
            if (id > 0) {
                boolean status = adminUserDao.getStatusById(id);
                String pwd = adminUserDao.getPwdById(id);
                int roleId = adminUserDao.getRoleIdById(id);
                boolean roleStatus = roleDao.isStatus(roleId);
                if (roleStatus) {
                    if (pwd.equals(MD5Utils.md5(userPwd + ParamUtils.SECRET))) {
                        if (status) {
                            JOptionPane.showMessageDialog(instance, "登录成功");
                            instance.setVisible(false);
                            UserManageView.getInstance().setVisible(true);
                        } else {
                            JOptionPane.showMessageDialog(instance, "该账号已被禁用,请联系管理员");
                        }
                    } else {
                        JOptionPane.showMessageDialog(instance, "密码有误,请检查");
                    }
                } else {
                    JOptionPane.showMessageDialog(instance, "该账号已被禁用,请联系管理员");
                }

            } else {
                JOptionPane.showMessageDialog(instance, "用户不存在,请联系超级管理员");
            }
        } else {
            JOptionPane.showMessageDialog(instance, "请检查!");
        }
    }
}
